/* -----------------------------------------------------------------------
 * Don't forget to include the following lines in your makefile:
 *
 * testFileName: testFileName.c dominion.o rngs.o
 *      gcc -o testFileName -g  testFileName.c dominion.o rngs.o $(CFLAGS)
 * -----------------------------------------------------------------------
 *
 *  testing the adventurer card
 */

#include "dominion.c"
#include "dominion.h"
#include "dominion_helpers.h"
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include "rngs.h"

// set NOISY_TEST to 0 to remove printfs from output
#define NOISY_TEST 1
#define NUM_TESTS 10

int cardCount(struct gameState *g, int player, int thisCard){
    int c,j;
    c = 0;

    for(j = 0; j < g->handCount[player]; j++){
        if(g->hand[player][j] == thisCard)
            c++;
    }
    for(j = 0; j < g->deckCount[player]; j++){
        if(g->deck[player][j] == thisCard)
            c++;
    }
    for(j = 0; j < g->discardCount[player]; j++){
        if(g->discard[player][j] == thisCard)
            c++;
    }

    return c;
}

int handCardCount(struct gameState *g, int player, int thisCard){
    int c,j;
    c = 0;

    for(j = 0; j < g->handCount[player]; j++){
        if(g->hand[player][j] == thisCard)
            c++;
    }

    return c;
}

int main() {
    int i,j;
    int seed = 1000;
    int numPlayer;
    int p, q, r, precount, postcount, thisPos, game;
    int k[10] = {adventurer, council_room, feast, gardens, mine
               , remodel, smithy, village, baron, great_hall};
    struct gameState G;

    srand(time(NULL));


#if (NOISY_TEST == 1)
    printf ("TESTING adventurer card:\n");
#endif
    for (i = 0; i < NUM_TESTS; i++)
    {
#if (NOISY_TEST == 1)
        printf("Test number %d.\t", i);
#endif
        numPlayer = (rand() % MAX_PLAYERS); //use random # players
        game = initializeGame(numPlayer, k, seed, &G); // initialize a new game
        assert(game == 0);
        int numCopper[numPlayer];
        int numSilver[numPlayer];
        int numGold[numPlayer];

        for(p = 0; p < numPlayer; p++){
#if (NOISY_TEST == 1)
            printf("Test player %d.\n", p);
#endif
            G.whoseTurn = p;
            r = 0;
            if(G.handCount[p] == 0){
                while(r < (rand() % MAX_HAND)){
                    drawCard(p,&G);
                }
            }
            numCopper[p] = cardCount(&G,p,copper); //total coppers for that player
            numSilver[p] = cardCount(&G,p,silver); //total silvers
            numGold[p] = cardCount(&G,p,gold); //total gold
            precount = handCardCount(&G,p,copper) + handCardCount(&G,p,silver) + handCardCount(&G,p,gold); //# treasures in hand before
            thisPos = rand() % G.handCount[p]; //random # position for adventurer
            //printf("numCopper = %d, numSilver = %d, numGold = %d, precount = %d, thisPos = %d\n",numCopper[p],numSilver[p],numGold[p],precount, thisPos);
            q = cardEffect(adventurer,0,0,0,&G,thisPos,0);
            assert(q == 0);
            //printf("numCopper = %d, numSilver = %d, numGold = %d, postcount = %d, thisPos = %d\n",numCopper[p],numSilver[p],numGold[p],postcount, thisPos);
            assert(numCopper[p] == cardCount(&G,p,copper)); //total copper hasn't changed
            assert(numSilver[p] == cardCount(&G,p,silver)); //total silver hasn't changed
            assert(numGold[p] == cardCount(&G,p,gold)); //total gold hasn't changed
            postcount = handCardCount(&G,p,copper) + handCardCount(&G,p,silver) + handCardCount(&G,p,gold);
            if((numCopper[p] + numSilver[p] + numGold[p]) >= (precount + 2)){
                assert(postcount == (precount+2)); //if there were at least 2 treasures not in hand before, 2 should have been added
            }
            else if((numCopper[p] + numSilver[p] + numGold[p]) > precount){
                assert(postcount == (precount+1)); //if there was only 1 treasure not in hand before, it should be in hand now
            }
            else{
                assert(postcount == precount); //if all treasures were in hand before, they should not have moved            }
            }
#if (NOISY_TEST == 1)
            printf("Test passed!\n");
#endif
        }
    }

    printf("All tests passed!\n");

    return 0;
}
/*reference code
adventurer: draws until 2 treasures have been drawn
non-treasures drawn are discarded
case adventurer:
      while(drawntreasure<2){
	if (state->deckCount[currentPlayer] <1){//if the deck is empty we need to shuffle discard and add to deck
	  shuffle(currentPlayer, state);
	}
	drawCard(currentPlayer, state);
	cardDrawn = state->hand[currentPlayer][state->handCount[currentPlayer]-1];//top card of hand is most recently drawn card.
	if (cardDrawn == copper || cardDrawn == silver || cardDrawn == gold)
	  drawntreasure++;
	else{
	  temphand[z]=cardDrawn;
	  state->handCount[currentPlayer]--; //this should just remove the top card (the most recently drawn one).
	  z++;
	}
      }
      while(z-1>=0){
	state->discard[currentPlayer][state->discardCount[currentPlayer]++]=temphand[z-1]; // discard all cards in play that have been drawn
	z=z-1;
      }
      return 0;
      */
